package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Html5;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteImageView;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private CuteTextView txtUserName;
    private RoundedImageView imageUserHead;
    private ImageView imageSex;

    private ImageView imageNewPoint;

    private CuteButton btnHardware, btnFav, btnYhq, btnOrder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(contentView == null) {
            contentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_mine, null);
            setLayout();
        }
        return contentView;
    }

    // 初始化视图
    private void setLayout() {
        imageUserHead = (RoundedImageView) contentView.findViewById(R.id.image_head);
        txtUserName = (CuteTextView) contentView.findViewById(R.id.text_user_name);
        imageSex = (ImageView) contentView.findViewById(R.id.image_sex);
        imageNewPoint = (ImageView) contentView.findViewById(R.id.icon_new);

        // 硬件
        btnHardware = (CuteButton) contentView.findViewById(R.id.btn_hardware);
        btnHardware.setOnClickListener(this);
        // 收藏
        btnFav = (CuteButton) contentView.findViewById(R.id.btn_fav);
        btnFav.setOnClickListener(this);
        // 优惠券
        btnYhq = (CuteButton) contentView.findViewById(R.id.btn_yhq);
        btnYhq.setOnClickListener(this);
        // 订单
        btnOrder = (CuteButton) contentView.findViewById(R.id.btn_order);
        btnOrder.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();

        // 是否有新通知
        showNewNotifyIcon(Config.getHasNewNotify(getContext()));

        // 登录信息
        LoginInfo loginInfo = Config.getLoginInfo(getContext());
        if(loginInfo == null) {
            return;
        }
        ImageLoader.getInstance().displayImage(loginInfo.getUrl(), imageUserHead, Config.getOptionsHead());
        txtUserName.setText(loginInfo.getName());
        if(loginInfo.getGender() == 1) {
            imageSex.setVisibility(View.VISIBLE);
            imageSex.setImageResource(R.mipmap.icon_sex_nan);
        } else if(loginInfo.getGender() == 2) {
            imageSex.setVisibility(View.VISIBLE);
            imageSex.setImageResource(R.mipmap.icon_sex_nv);
        } else {
            imageSex.setVisibility(View.INVISIBLE);
        }
    }

    // 显示新通知的图标
    public void showNewNotifyIcon(boolean flag) {
        if(imageNewPoint == null) return;
        if(flag) {
            imageNewPoint.setVisibility(View.VISIBLE);
        } else {
            imageNewPoint.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getContext(), Html5Activity.class);
        switch (v.getId()) {
            case R.id.btn_hardware:
                intent.putExtra("url", Html5.MINE_HARDWARE);
                break;
            case R.id.btn_fav:
                intent.putExtra("url", Html5.MINE_FAVORITE);
                break;
            case R.id.btn_yhq:
                intent.putExtra("url", Html5.MINE_YOUHUIQUAN);
                break;
            case R.id.btn_order:
                intent.putExtra("url", Html5.MINE_ORDER);
                break;
        }
        startActivity(intent);
    }
}
